<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub CustomCookieWrite
 '== Create a cookie with the given attributes. 
 '== If the attributes are not supplied use defaults.
	Dim sPath
	Dim	sDomain
	Dim sName
	Dim sValue
     
	sPath = "/"
	sDomain = "?"
	sName = "MyLifeCustomCookie"
	sValue = "1234"
	   
	Response.Cookies(sName) = sValue
	Response.Cookies(sName).Path = sPath
	'Response.Cookies(sName).Domain = sDomain
	
End Sub
Sub BBCookieWrite
 '== Create a cookie with the given attributes for Blue bean. 
 '== If the attributes are not supplied use defaults.
	Dim sPath
	Dim	sDomain
	Dim sName
	Dim sValue
     
	sPath = "/"
	sDomain = "?"
	sName = "MyLifeBlueBeanCookie"
	sValue = "1234"
	   
	Response.Cookies(sName) = sValue
	Response.Cookies(sName).Path = sPath
	'Response.Cookies(sName).Domain = sDomain
	
End Sub


Public Function bCheckCustomCookie
	Dim sName
	
	sName = "MyLifeCustomCookie"
		
	If Request.Cookies(sName) = "" then 
		'response.write "false"	
		bCheckCustomCookie = "false"
	else
		'response.write "true"
		bCheckCustomCookie = "true"
	end if

End Function
Public Function bCheckBBCookie
	Dim sName
	
	sName = "MyLifeBlueBeanCookie"
		
	If Request.Cookies(sName) = "" then 
		'response.write "false"	
		bCheckBBCookie = "false"
	else
		'response.write "true"
		bCheckBBCookie = "true"
	end if

End Function

Public Function GetBBSession(sName)
	if bCheckBBCookie = "true" then
		GetBBSession=GetSessionInfo(sName)
	else
		GetBBSession=""
	end if

End Function

Sub SetSessionInfo(SessionName,SessionValue)
Dim user
	'Wrapper Sub to Set Session information

	'This needs to be replaced to use site server info.
	'For the mean time this will be implemented using session variables

	'Session(SessionName)=SessionValue
	'*************************************************

	if isnull(SessionValue) then SessionValue=""
	SessionValue = Cstr(SessionValue)
	Err.Clear 'Need to clear errors collection for AOU to work correctly	
	'Create AUO Object
	Set user = Server.CreateObject("Membership.UserObjects")
	'Error check
	if Err.number <> 0 then
		Response.Write "AUO object fails"
	End If
    'Check if we have a session for this user
	If not IsArray(user("SessionState").objectClass) Then
		user("SessionState").objectClass = Array("SessionState", "dynamicObject")
		User("SessionState").entryTTL = 1800
	End If
    'Error check
	if Err.number <> 0 then
		Response.Write "Cannot assign to Session State object"
	end if
	User("SessionState").SetInfo
	
	If lCase(SessionName) = "lpersonid" then User("SessionState").lPersonId = SessionValue
	If lCase(SessionName) = "lclientid" then User("SessionState").lClientId = SessionValue
	If lCase(SessionName) = "sregistered" then 
			if ucase(SessionValue)<>"R" and  ucase(SessionValue)<>"A" then
					exit sub ' only set to R,A
			End If
			User("SessionState").sRegistered = SessionValue
	End if

	If lCase(SessionName) = "luserid" then User("SessionState").lUserId = SessionValue
	If lCase(SessionName) = "errordesc" then User("SessionState").errordesc = SessionValue
	If lCase(SessionName) = "dtlastaccessed" then User("SessionState").dtLastAccess = SessionValue
	If lCase(SessionName) = "slogon" then User("SessionState").slogon = SessionValue
	If lCase(SessionName) = "sclientname" then User("SessionState").sclientname = SessionValue
	If lCase(SessionName) = "laccesslevel" then User("SessionState").laccesslevel = SessionValue
 	If lCase(SessionName) = "frombb" then User("SessionState").frombb = SessionValue
 	If lCase(SessionName) = "bbcard" then User("SessionState").bbcard = SessionValue

	User("SessionState").SetInfo
	set User = nothing	
	
	
	'*************************************************
	
	
	
End Sub
Function GetSessionInfo(SessionName)
Dim sTemp
Dim user

	if bCheckCustomCookie = "false" then 
		if lcase(SessionName)<>"errordesc" then
			GetSessionInfo=""
			exit function
		end if
	end if
	
	Err.Clear 'Need to clear errors collection for AOU to work correctly
	'Wrapper Sub to Return Session information for a particular field

	'This needs to be replaced to use site server info.
	'For the mean time this will be implemented using session variables

	'GetSessionInfo = Session(SessionName)
	Set user = Server.CreateObject("Membership.UserObjects")

	if Err.number <> 0 then
		Response.Write "AUO object fails"
	End If
  
	If not IsArray(user("SessionState").objectClass) Then
		user("SessionState").objectClass = Array("SessionState", "dynamicObject")
	End If
	  
	User("SessionState").entryTTL = 1800
	if Err.number <> 0 then
		Response.Write "Cannot assign to Session State object"
	end if
	User("SessionState").SetInfo
  
	if lcase(SessionName)= "lpersonid" then sTemp = User("SessionState").lPersonId
	if lcase(SessionName)= "luserid" then sTemp = User("SessionState").lUserId
	if lcase(SessionName)= "lclientid" then sTemp = User("SessionState").lClientId
	if lcase(SessionName)= "sregistered" then sTemp = User("SessionState").sRegistered
	if lcase(SessionName)= "errordesc" then sTemp = User("SessionState").errordesc
	If lCase(SessionName) = "dtlastaccessed" then sTemp = User("SessionState").dtLastAccess
	If lCase(SessionName) = "slogon" then sTemp = User("SessionState").slogon
	If lCase(SessionName) = "sclientname" then sTemp = User("SessionState").sclientname
	If lCase(SessionName) = "laccesslevel" then sTemp = User("SessionState").laccesslevel
 	If lCase(SessionName) = "frombb" then sTemp = User("SessionState").frombb
 	If lCase(SessionName) = "bbcard" then sTemp = User("SessionState").bbcard
	
	GetSessionInfo = sTemp
End Function




Sub CheckSession 'Check to see if a client is logged in, else redirect to login
	Err.Clear 'Need to clear errors collection for AOU to work correctly
	If GetSessionInfo("lAccessLevel") = "l" then 'check for hacker bypassing matrix test
		response.redirect "/mylife/pages/logon/MatrixNoAccess.asp"
		response.end
	end if
	If GetSessionInfo("lPersonId")="" OR GetSessionInfo("lUserId")="" OR  GetSessionInfo("lClientId")="" OR GetSessionInfo("sRegistered")="A"  OR bCheckCustomCookie = "false" then
		response.redirect "/Mylife/Pages/Logon/ForceLogoff.asp"
		response.end
		'CreateNewRegistration()
	End if
End sub

Sub CheckMatrixSession 'Check to see if a client is logged in and is needing to do a matrix test, else redirect to login
	Err.Clear 'Need to clear errors collection for AOU to work correctly
	'If GetSessionInfo("lAccessLevel") = "l" then 'check for hacker bypassing matrix test
	'	response.redirect "/mylife/pages/logon/MatrixTest.asp"
	'	response.end
	'end if
	If GetSessionInfo("lPersonId")="" OR GetSessionInfo("lUserId")="" OR  GetSessionInfo("lClientId")="" OR GetSessionInfo("sRegistered")="A"  OR bCheckCustomCookie = "false" then
		response.redirect "/Mylife/Pages/Logon/ForceLogoff.asp"
		response.end
	End if
End sub

Sub CreateNewRegistration
	Err.Clear 'Need to clear errors collection for AOU to work correctly
	Dim lPersonId, lUserId, lClientId, mtsObj, lAccessLevel 
	If GetSessionInfo("lAccessLevel") = "l" then 'check for hacker bypassing matrix test
		response.redirect "/mylife/pages/logon/MatrixNoAccess.asp"
		response.end
	end if
	If GetSessionInfo("lPersonId")="" OR GetSessionInfo("lUserId")="" OR  GetSessionInfo("lClientId")=""  OR bCheckCustomCookie = "false" then
	'Some Session variables are missing OR the cookie KEY is missing: This is anonymous user. Getsessions for him
		lPersonId=""
		lUserId=""
		lClientId=""
		Set mtsObj = Server.Createobject("ML_Registration.clsRegistration")
		mtsObj.RegisterAnon lClientId,lUserId , lPersonId
		SetSessionInfo "lPersonId",lPersonID
		SetSessionInfo "lUserId",lUserID
		SetSessionInfo "lClientId",lClientID
		SetSessionInfo "sRegistered","A" 
		SetSessionInfo "sClientName","Anon"
		SetSessionInfo "sLogon","Anon"
		SetSessionInfo "dtLastAccessed",""
 		SetSessionInfo "bbcard",""
		CustomCookieWrite 'Set the key to now allow access
		
	end if
End Sub
Sub SessionExpired 
	if trim(GetSessionInfo("lClientId")) = "" then
			'Response.Redirect "/mylife/pages/Logon/Login.asp"
			Response.Redirect "/mylife/index.asp"
	end if
end sub

'Check if the mail is from DEV/TEST or PROD
sub CheckEmail(sEmail)
	Dim objAdminDef, bDevMail

	Set objAdminDef = server.CreateObject("ML_AdminDefaults.clsAdminDefaults")
    bDevMail = objAdminDef.bGetDevMail
    If Len(sEmail) > 17 Then
        If bDevMail Then
            response.write sEmail
        Else
            If (LCase(Right(Trim(sEmail), 13)) = "@mylife.co.za") Or (LCase(Right(Trim(sEmail), 17)) = "@mylifemail.co.za") Then
                response.write "customercare@pilex.liberty.co.za"
            End If
        End If
    Else
        If bDevMail Then
            response.write sEmail
        Else
            If LCase(Right(Trim(sEmail), 13)) = "@mylife.co.za" Then
                response.write "customercare@pilex.liberty.co.za"
            End If
        End If
    End If
    
    set objAdminDef = nothing
end sub

'Check FAQ answers for email addresses
sub CheckFAQEmailString(sText)
	dim iCount, sLeft, sRight, iLen, iTemp
	Dim objAdminDef, bDevMail
	
	Set objAdminDef = server.CreateObject("ML_AdminDefaults.clsAdminDefaults")
    bDevMail = objAdminDef.bGetDevMail
	
	if not bDevMail then
		for iCount = 1 to len(sText)
			If mid(sText, iCount, 1) = ":" then
				if lcase(mid(sText, iCount - 6, 6)) = "mailto" then
					iLen = 0
					iTemp = iCount
					sRight = ""
					sLeft = ""
					while mid(sText, iCount, 1) <> """" and mid(sText, iCount, 1) <> " "
						iLen = iLen + 1
						iCount = iCount + 1
					wend
					sLeft = left(sText, iTemp)
					sRight = right(sText, len(sText) - len(sLeft) - iLen + 1)
					sText = sLeft & "customercare@pilex.liberty.co.za" & sRight
					iCount = iCount - iLen
					iCount = iCount + 32
				end if
			end if
		next
	end if
	response.write sText
	set objAdminDef = nothing
end sub

sub SetBBSession

		SetSessionInfo "lPersonId",""
		SetSessionInfo "lUserId",""
		SetSessionInfo "lClientId",""
		SetSessionInfo "sRegistered","A" 
		SetSessionInfo "sClientName","Anon"
		SetSessionInfo "sLogon","Anon"
		SetSessionInfo "dtLastAccessed",""
		SetSessionInfo "frombb","Y"
		CustomCookieWrite 'Set the key to now allow access
		BBCookieWrite

end sub


sub CheckLoginAllowed 'check to see if a user has mutiple browsers open
	dim sRegistered
	Dim user

	Err.Clear 'Need to clear errors collection for AOU to work correctly
	'Wrapper Sub to Return Session information for a particular field

	Set user = Server.CreateObject("Membership.UserObjects")

	if Err.number <> 0 then
		Response.Write "AUO object fails"
	End If
  
	If not IsArray(user("SessionState").objectClass) Then
		user("SessionState").objectClass = Array("SessionState", "dynamicObject")
	End If
	  
	if Err.number <> 0 then
		Response.Write "Cannot assign to Session State object"
	end if
	User("SessionState").SetInfo
  
	sRegistered =  User("SessionState").sRegistered
	
	if ucase(sRegistered) = "A" or sRegistered = "" then
		'CheckLoginAllowed = "Allowed"
	else
		'CheckLoginAllowed = "NotAllowed"
		Response.Redirect "/mylife/Pages/Logon/Multiple_Browsers.asp"
	end if
end sub

</SCRIPT>
